import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../widgets/inputWidget.dart';
import '../../helper/Helper.dart';

import '../../helper/channelHelper.dart';

import '../../helper/uidata.dart';

import 'jxgzp_authorty.dart';

import '../../widgets/signatureCard2.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../helper/global_config.dart';

import '../../model/jxgzp.dart';
import '../../widgets/IconWidget.dart';

class JXGZPInfoPage extends StatefulWidget {
  @override
  JXGZPInfoPage(this.s1, this.callback);
  final JXGZP s1;
  final RefrushCallback callback;

  @override
  createState() => new JXGZPInfoPageState();
}

class JXGZPInfoPageState extends State<JXGZPInfoPage> {
  late JXGZP s1;
  final TextEditingController c2 = new TextEditingController();

  @override
  void initState() {
    super.initState();
    //s1 = widget.s1;
    //UIData.CurrentUser!.Authority="水调值班员,电厂值班员";
    loadData();
  }

  loadData() async {
    var s2 = await JXGZP.GetByID(widget.s1.ID);
    print(s2!.BMIfOK);
    setState(() {
      s1 = s2;
    });
  }

  _callPhone(String p) async {
    await launch("tel:$p");
  }

  upload(String imgId) async {
    s1 = await JXGZP.GetByID(widget.s1.ID) as JXGZP;
    print(s1.toJson());
    if (s1.State == 0 && s1.BMUserID == UIData.CurrentUser!.Guid) {
      s1.BMIfOK = true;
      s1.BMTM = Helper.DateTimeNowString();
      s1.BMQM = imgId;

      s1.State = 1;
      await JXGZP.SendById(s1.Title, "需要会签", s1.DCUserID);
      await JXGZP.SendById(s1.Title, "需要会签", s1.SDUserID);
    }

    if (s1.State == 1 && s1.DCUserID == UIData.CurrentUser!.Guid) {
      s1.DCIfOK = true;
      s1.DCTM = Helper.DateTimeNowString();
      s1.DCQM = imgId;

      if (s1.SDIfOK) {
        s1.State = 2;
        await JXGZP.SendById(s1.Title, "需要签发", s1.SDLDUserID);
        await JXGZP.SendById(s1.Title, "需要签发", s1.DCLDUserID);
        await JXGZP.SendById(s1.Title, "需要签发", s1.GCBLDUserID);
      }
    }

    if (s1.State == 1 && s1.SDUserID == UIData.CurrentUser!.Guid) {
      s1.SDIfOK = true;
      s1.SDTM = Helper.DateTimeNowString();
      s1.SDQM = imgId;

      if (s1.DCIfOK) {
        s1.State = 2;
        await JXGZP.SendById(s1.Title, "需要签发", s1.SDLDUserID);
        await JXGZP.SendById(s1.Title, "需要签发", s1.DCLDUserID);
        await JXGZP.SendById(s1.Title, "需要签发", s1.GCBLDUserID);
      }
    }

    if (s1.State == 2) {
      if (s1.SDLDUserID == UIData.CurrentUser!.Guid) {
        s1.SDLDIfOK = true;
        s1.SDLDTM = Helper.DateTimeNowString();
        s1.SDLDQM = imgId;
      }
      if (s1.DCLDUserID == UIData.CurrentUser!.Guid) {
        s1.DCLDIfOK = true;
        s1.DCLDTM = Helper.DateTimeNowString();
        s1.DCLDQM = imgId;
      }
      if (s1.GCBLDUserID == UIData.CurrentUser!.Guid) {
        s1.GCBLDIfOK = true;
        s1.GCBLDTM = Helper.DateTimeNowString();
        s1.GCBLDQM = imgId;
      }

      if (s1.DCLDIfOK && s1.SDLDIfOK && s1.GCBLDIfOK) {
        s1.State = 3;
        await JXGZP.SendById(s1.Title, "已签发完成！", s1.BMUserID);
      }
    }

    await JXGZP.Update(s1);
    await loadData();
    widget.callback();
    ChannelHelper.showToast("签字成功！");
  }

  List<Widget> getWidgetList() {
    List<Widget> list1 = <Widget>[];
    // list1.add(InputWidget.getUserTile(context, "工作票审核", s1.BMUserID));
    // list1.add(InputWidget.getQMTile(s1.BMIfOK, Helper.DateTimeFormat(s1.BMTM), s1.BMQM));
    list1.add(InputWidget.getQMTile2(
        "工作票审核", s1.BMIfOK, s1.BMQM, Helper.DateTimeFormat(s1.BMTM)));

    if (!s1.BMIfOK) return list1;
    list1.add(Divider(
      height: 0.0,
    ));
    list1.add(InputWidget.getQMTile2(
        "电厂会签", s1.DCIfOK, s1.DCQM, Helper.DateTimeFormat(s1.DCTM)));
    // list1.add(InputWidget.getUserTile(context, "电厂会签", s1.DCUserID));
    // list1.add(InputWidget.getQMTile(s1.DCIfOK, Helper.DateTimeFormat(s1.DCTM), s1.DCQM));
    list1.add(Divider(
      height: 0.0,
    ));
    list1.add(InputWidget.getQMTile2(
        "水调中心会签", s1.SDIfOK, s1.SDQM, Helper.DateTimeFormat(s1.SDTM)));

    // list1.add(InputWidget.getUserTile(context, "水调中心会签", s1.SDUserID));
    // list1.add(InputWidget.getQMTile(s1.SDIfOK, Helper.DateTimeFormat(s1.SDTM), s1.SDQM));
    list1.add(Divider(
      height: 0.0,
    ));

    return list1;
  }

  List<Widget> getWidgetList0() {
    List<Widget> list1 = <Widget>[];
    list1.add(InputWidget.getInfo("泄水过程简述", s1.XieShuiGC));
    list1.add(Divider(
      height: 0.0,
    ));
    list1.add(InputWidget.getInfo("工作内容简述", s1.Content));
    list1.add(Divider(
      height: 0.0,
    ));
    return list1;
  }

  List<Widget> getWidgetList2() {
    List<Widget> list1 = <Widget>[];
    if (s1.State < 2) return list1;

    // list1.add(InputWidget.getUserTile(context, "电厂分管领导", s1.DCLDUserID));
    // list1.add(
    //     InputWidget.getQMTile(s1.DCLDIfOK, Helper.DateTimeFormat(s1.DCLDTM), s1.DCLDQM));

    list1.add(InputWidget.getQMTile2(
        "电厂分管领导", s1.DCLDIfOK, s1.DCLDQM, Helper.DateTimeFormat(s1.DCLDTM)));

    list1.add(Divider(
      height: 0.0,
    ));

    // list1.add(InputWidget.getUserTile(context, "监测中心分管领导", s1.GCBLDUserID));
    // list1.add(InputWidget.getQMTile(
    //     s1.GCBLDIfOK, Helper.DateTimeFormat(s1.GCBLDTM), s1.GCBLDQM));
    list1.add(InputWidget.getQMTile2("监测中心分管领导", s1.GCBLDIfOK, s1.GCBLDQM,
        Helper.DateTimeFormat(s1.GCBLDTM)));

    list1.add(Divider(
      height: 0.0,
    ));
    list1.add(InputWidget.getQMTile2(
        "水调中心分管领导", s1.SDLDIfOK, s1.SDLDQM, Helper.DateTimeFormat(s1.SDLDTM)));

    // list1.add(InputWidget.getUserTile(context, "水调中心分管领导", s1.SDLDUserID));
    // list1.add(
    //     InputWidget.getQMTile(s1.SDLDIfOK, Helper.DateTimeFormat(s1.SDLDTM), s1.SDLDQM));
    list1.add(Divider(
      height: 0.0,
    ));

    return list1;
  }

  void showPanel(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SignatureCard2Page(
            upload, JxgzpAuthority.QianZiTitle(s1, UIData.CurrentUser!));
      },
    );
  }

  Widget getFloatingButton(BuildContext context) {
    if (s1 == null) return new Container();
    if (JxgzpAuthority.IfQianZi(s1, UIData.CurrentUser!)) {
      return FloatingActionButton(
          child: Icon(FontAwesomeIcons.penAlt),
          onPressed: () {
            showPanel(context);
          });
    }

    return new Container();
  }

  @override
  Widget build(BuildContext context) {
    // if (s1 == null) return Center(child:SpinKitCircle(color: Colors.blue,));

    // TODO: implement build
    return Scaffold(
        // appBar: AppBar(title: Text('工作票详情')),
        //bottomSheet:bottomPanel2() ,
        floatingActionButton: getFloatingButton(context),
        body: s1.ID == 0
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : SingleChildScrollView(
                child: new Container(
                    margin: EdgeInsets.only(top: 8.0),
                    child: new Column(children: <Widget>[
                      Card(
                        child: ListTile(
                          leading: IconWidget.JXGZPImage(),
                          title: Text(s1.Title),
                        ),
                        color: GlobalConfig.cardBackgroundColor,
                        margin: const EdgeInsets.all(6.0),
                      ),
                      Card(
                        child: Column(
                          children: <Widget>[
                            ListTile(
                              title: Text("泄水过程简述"),
                            ),
                            ListTile(
                              title: Text(
                                s1.XieShuiGC,
                                style: TextStyle(
                                    color: Colors.black54, height: 1.6),
                              ),
                              // subtitle: Text(Helper.DateTimeFormat(s1.CreateTime)),
                            ),
                            Divider(
                              height: 0.0,
                            ),
                            ListTile(
                              title: Text("工作内容简述"),
                            ),
                            ListTile(
                              title: Text(
                                s1.Content,
                                style: TextStyle(
                                    color: Colors.black54, height: 1.6),
                              ),
                              // subtitle: Text(Helper.DateTimeFormat(s1.CreateTime)),
                            ),
                          ],
                        ),
                        color: GlobalConfig.cardBackgroundColor,
                        margin: const EdgeInsets.all(6.0),
                      ),
                      Card(
                        child: Column(
                          children: <Widget>[
                            ListTile(
                              trailing: Text(
                                s1.BeginTime,
                                style: TextStyle(
                                    color: Colors.black54, height: 1.0),
                              ),
                              title: Text(
                                "计划开始时间",
                              ),
                            ),
                            Divider(
                              height: 0.0,
                            ),
                            ListTile(
                              trailing: Text(
                                s1.EndTime,
                                style: TextStyle(
                                    color: Colors.black54, height: 1.0),
                              ),
                              title: Text(
                                "计划结束时间",
                              ),
                            ),
                            Divider(
                              height: 0.0,
                            ),
                            ListTile(
                              title: Text("闸门操作要求"),
                            ),
                            ListTile(
                              title: Text(
                                s1.ZhaMenYaoQiu,
                                style: TextStyle(
                                    color: Colors.black54, height: 1.6),
                              ),
                              // subtitle: Text(Helper.DateTimeFormat(s1.CreateTime)),
                            ),
                            Divider(
                              height: 0.0,
                            ),
                            ListTile(
                              title: Text(
                                Helper.DateTimeFormat(s1.CreateTime),
                                style: TextStyle(
                                    color: Colors.black54, height: 1.0),
                              ),
                              trailing: Text(
                                s1.ZBYName,
                                style: TextStyle(
                                    color: Colors.black54, height: 1.0),
                              ),
                            ),
                          ],
                        ),
                        color: GlobalConfig.cardBackgroundColor,
                        margin: const EdgeInsets.all(6.0),
                      ),
                      Card(
                        child: Column(
                          children: getWidgetList(),
                        ),
                        color: GlobalConfig.cardBackgroundColor,
                        margin: const EdgeInsets.all(6.0),
                      ),
                      Card(
                        child: Column(
                          children: getWidgetList2(),
                        ),
                        color: GlobalConfig.cardBackgroundColor,
                        margin: const EdgeInsets.all(6.0),
                      ),
                    ])),
              ));
  }
}
